// pages/buy/create/create.js
let api = require('../../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var datas = wx.getStorageSync("datas");
    console.log(datas)
    api.newpayorder(datas, (res) => {
      console.log(res)
      var result = res.data.result
      var discountPrice = 0
      discountPrice = result.totalprice - result.coupon_price
      result.wallet_price = 0
      this.setData({
        createData: res.data.result,
        discountPrice: discountPrice
      })
    })
  },
  creaData() {
    let type = this.data.createData.type
    let list = this.data.createData.list
    let listData = []
    if (type == 1) {
      list.forEach((item) => {
        listData.push({
          "num": item.num,
          "sc_id": item.id,
          "d_id": this.data.createData.address.d_id,
          "type": type
        })
      })
    } else {
      list.forEach((item) => {
        listData.push({
          "num": item.num,
          "sc_id": item.sc_id,
          "d_id": this.data.createData.address.d_id,
          "type": type
        })
      })
    }
    return listData
  },
  goPlay() {
    let type = this.data.createData.type
    let arr = this.creaData();
    if (this.data.discountPrice == 0){
      wx.showToast({
        title: '支付成功',
        icon: 'none',
        duration: 2000
      })
    }
    // console.log(this.data.createData)
    let arrs = {};
    arrs.list = arr
    if (this.data.createData.coupon_id != 0){
      arrs.coupon_id = this.data.createData.coupon_id
    }
    if(this.data.checked){
      arrs.user_price = 1
    }
    // arrs = {
    //   list: arr,
    //   coupon_id: 2     // 等于0的时候不传
    //   // user_price: 0     //  选择钱包传 1   不选不传。
    // };
    if (this.data.createData.address.address == 0) {
      wx.showToast({
        title: '需要添加地址哦',
        icon: "none"
      })
      return false
    }
    api.pay(arrs, (res) => {
      let order_sn = res.data.result.order_sn
      console.log(res, order_sn)
      wx.requestPayment({
        'timeStamp': res.data.result.timeStamp,
        'nonceStr': res.data.result.nonceStr,
        'package': res.data.result.package,
        'signType': 'MD5',
        'paySign': res.data.result.paySign,
        'success': function(res) {
          console.log(res, '支付成功')
          wx.navigateTo({
            url: `/pages/my/orderContent/orderContent?id=${order_sn}`,
          })
        },
        'fail': function(res) {
          wx.showToast({
            title: '支付失败',
            icon: "none"
          })
          wx.navigateTo({
            url: `/pages/my/orderContent/orderContent?id=${order_sn}`,
          })
        },
        'complete': function(res) {}
      })
    })
  },
  url() {
    console.log(132)
    wx.navigateTo({
      url: `/pages/buy/buySite/buySite`,
    })
  },
  dataUrl() {
    wx.navigateTo({
      url: `/pages/buy/address/address?type=1`,
    })
  },
  discounts(e) {
    var totalprice = e.currentTarget.dataset.totalprice
    var coupon_id = e.currentTarget.dataset.coupon_id
    console.log(e)
    if (coupon_id == 0) {
      wx.navigateTo({
        url: '/pages/buy/discounts/discounts?price=' + totalprice,
      })
    } else {
      wx.navigateTo({
        url: '/pages/buy/discounts/discounts?price=' + totalprice + "&id=" + coupon_id,
      })
    }
  },

  switchChange: function(e) {
    // console.log(e.detail.value)
    var that = this
    var createData = this.data.createData
    var value = e.detail.value
    var discountPrice = that.data.discountPrice
    if (value) {
      discountPrice = parseFloat(createData.totalprice) - parseFloat(createData.coupon_price) - parseFloat(createData.user_price)
    } else {
      discountPrice = parseFloat(createData.totalprice) - parseFloat(createData.coupon_price)
    }
    
    // discountPrice = discountPrice.toFixed(2)
    discountPrice = Math.floor(discountPrice * 100) / 100
    console.log(discountPrice)
    if (createData.user_price  > (parseFloat(createData.totalprice) - parseFloat(createData.coupon_price))){
      createData.wallet_price = parseFloat(createData.totalprice) - parseFloat(createData.coupon_price)
    }else{
      createData.wallet_price = createData.user_price
    }
    console.log(createData)
    that.setData({
      checked:value,
      discountPrice: discountPrice < 0 ? 0 : discountPrice,
      createData: createData
    })
  }
})